package com.shop2cn.iapi.member.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.CreateMemberBean;
import com.shop2cn.iapi.member.parameter.MchSendPointsBean;
import com.shop2cn.iapi.member.service.MchSendPointsCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmember.MemberPoints;
import com.ymttest.database.model.godmember.PointsEntry;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;
import com.ymttest.utils.TestDataManager;
/**
*商家给会员送积分
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_MchSendPoints{
	private static MchSendPointsBean mchsendpointsBean;
	private static MchSendPointsCall mchsendpointsCall;
	GodMemberWapper GodMemberWapper;
	private static Integer shopid ;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家给会员送积分");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		mchsendpointsBean=new MchSendPointsBean();
		mchsendpointsCall=new MchSendPointsCall();
		GodMemberWapper=new GodMemberWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(mchsendpointsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MchSendPoints_001() {
		Logger.start(true,"商家给会员送积分成功");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			mchsendpointsBean.setMemberId(memid);
			mchsendpointsBean.setPoints(10);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("200", mchsendpointsCall.getString("code"), "验证code");
			
			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals(mpdb.getAvailablePoints(), mchsendpointsBean.getPoints(), "验证getAvailablePoints");
			Logger.verifyEquals(mpdb.getShopId(), mchsendpointsBean.getShopId(), "验证getShopId");
			Logger.verifyEquals(mpdb.getTotalPoints(), mchsendpointsBean.getPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			PointsEntry entrydb = Entrylist.stream().filter(x->{return x.getBizType().equals(200000);}).findFirst().get();
//			[{"entryId":63,"memberId":"HY20200521001326","bizType":200000,"bizId":"c62523d2d75e466493ef30c28e58873e","changePoints":10,"points":10,"pointsTime":"2020-05-21T18:50:54","operator":"主账号","remark":"test932","createTime":"2020-05-21T18:50:54","updateTime":"2020-05-21T18:50:54"}]
			Logger.verifyEquals(entrydb.getChangePoints(), mchsendpointsBean.getPoints(), "验证ChangePoints");
			Logger.verifyEquals(entrydb.getPoints(), mchsendpointsBean.getPoints(), "验证points");
			Logger.verifyEquals(entrydb.getOperator(), "主账号", "验证Operator");
			Logger.verifyEquals(entrydb.getRemark(), mchsendpointsBean.getRemark(), "验证remark");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchSendPoints_002() {
		Logger.start(true,"不输入shopid失败");
		try {
			mchsendpointsBean.setMemberId("1");
			mchsendpointsBean.setPoints(10);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
//			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("400", mchsendpointsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchSendPoints_003() {
		Logger.start(true,"不输入memberid失败");
		try {
//			mchsendpointsBean.setMemberId("1");
			mchsendpointsBean.setPoints(10);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("400", mchsendpointsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchSendPoints_004() {
		Logger.start(true,"shopid和memberid不匹配失败");
		try {
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			
			Integer shopid2 = GodmchManageCallService.CreateSellerMch();
			GodMemberService.setCollectionMobile(shopid2, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid2));
			mchsendpointsBean.setMemberId(memid);
			mchsendpointsBean.setPoints(10);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid2);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("300", mchsendpointsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchSendPoints_005() {
		Logger.start(true,"不输入points失败");
		try {
			mchsendpointsBean.setMemberId("1");
//			mchsendpointsBean.setPoints(10);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("300", mchsendpointsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchSendPoints_006() {
		Logger.start(true,"输入points为0失败");
		try {
			mchsendpointsBean.setMemberId("1");
			mchsendpointsBean.setPoints(0);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("300", mchsendpointsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchSendPoints_007() {
		Logger.start(true,"不输入remark失败");
		try {
			mchsendpointsBean.setMemberId("1");
			mchsendpointsBean.setPoints(0);
//			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("300", mchsendpointsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchSendPoints_008() {
		Logger.start(true,"输入points负数成功扣积分");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			GodMemberService.sendPoints(memid, shopid, 10);
			mchsendpointsBean.setMemberId(memid);
			mchsendpointsBean.setPoints(-7);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("200", mchsendpointsCall.getString("code"), "验证code");
			
			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals(mpdb.getAvailablePoints(), 10-7, "验证getAvailablePoints");
			Logger.verifyEquals(mpdb.getShopId(), mchsendpointsBean.getShopId(), "验证getShopId");
			Logger.verifyEquals(mpdb.getTotalPoints(), 10-7, "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			PointsEntry entrydb = Entrylist.stream().filter(x->{return x.getBizType().equals(210000);}).findFirst().get();
//			[{"entryId":63,"memberId":"HY20200521001326","bizType":200000,"bizId":"c62523d2d75e466493ef30c28e58873e","changePoints":10,"points":10,"pointsTime":"2020-05-21T18:50:54","operator":"主账号","remark":"test932","createTime":"2020-05-21T18:50:54","updateTime":"2020-05-21T18:50:54"}]
			Logger.verifyEquals(entrydb.getChangePoints(), mchsendpointsBean.getPoints(), "验证ChangePoints");
			Logger.verifyEquals(entrydb.getPoints(), 10-7, "验证points");
			Logger.verifyEquals(entrydb.getOperator(), "主账号", "验证Operator");
			Logger.verifyEquals(entrydb.getRemark(), mchsendpointsBean.getRemark(), "验证remark");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MchSendPoints_009() {
		Logger.start(true,"扣积分不能把总积分扣成负数");
		try {
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			GodMemberService.sendPoints(memid, shopid, 10);
			mchsendpointsBean.setMemberId(memid);
			mchsendpointsBean.setPoints(-17);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("300", mchsendpointsCall.getString("code"), "验证code");
			//积分不足，暂不可进行此操作
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MchSendPoints_010() {
		Logger.start(true,"未开启积分功能失败-店铺信息不存在");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			
			mchsendpointsBean.setMemberId(memid);
			mchsendpointsBean.setPoints(10);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("300", mchsendpointsCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MchSendPoints_011() {
		Logger.start(true,"商家给会员送积分-remark过长失败");
		try {
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			
			mchsendpointsBean.setMemberId(memid);
			mchsendpointsBean.setPoints(10);
			
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomNum(50));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("400", mchsendpointsCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MchSendPoints_012() {
		Logger.start(true,"未开启积分功能失败-积分未开启");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			//创建一个新会员
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			createMem.setOpenId(usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			GodMemberService.mchSetOpenPoints(shopid, false);
			mchsendpointsBean.setMemberId(memid);
			mchsendpointsBean.setPoints(10);
			mchsendpointsBean.setRemark("test"+TestDataManager.getRandomIntNum(3));
			mchsendpointsBean.setShopId(shopid);
			mchsendpointsCall.setData(mchsendpointsBean);
			mchsendpointsCall.callService();			
			Logger.verifyEquals("300", mchsendpointsCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}